<?php
/**
 * Created by PhpStorm.
 * User: nathan
 * Date: 2019/2/13
 * Time: 11:59
 */

namespace Refine\Iplookup;

/**
 * Class SinaIplookup
 * @package Iplookup
 */
class A126Iplookup extends AbstractIplookup
{

    /**
     * @param string $ip
     * @return string
     */
    public function lookup($ip)
    {
        return $this->parseResponse($this->curl->get('http://ip.ws.126.net/ipquery', ['ip' => $ip]));
    }

    /**
     * @param string $response
     * @return string
     */
    private function parseResponse($response)
    {
        $response = iconv('gb18030', 'utf-8', $response);
        if (preg_match('#lo="([^"]+)"#', $response, $m) === 1) {
            return trim($m[1], '省市');
        }
        return 'unknown';
    }
}